package com.springcloud.market.user1.service.impl;

import com.springcloud.market.user1.dto.UserDTO;
import com.springcloud.market.user1.pojo.User;
import com.springcloud.market.user1.service.UserRegisterMessageSender;
import com.springcloud.market.user1.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


//@Service
@Slf4j
@DubboService
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRegisterMessageSender userRegisterMessageSender;
    // 第三次课
    @Override
    public  String getUserInfo(){
        return "user借口已调取";
    }
    //    第三次作业
    @Override
    public  String getUserInfo1(){
        return "user借口已调取";
    }

    @Override
    public User registerUser(UserDTO userDTO) {
        // 模拟注册成功,自动生成ID
        String userId = "1001";
        log.info("用户注册成功，用户ID: {}", userId);
        // 2. 【新增】异步发送注册成功事件消息
        try {
            userRegisterMessageSender.sendUserRegisterMessage(
                    userId,
                    userDTO.getUsername(),
                    userDTO.getPhoneNumber()
            );
        } catch (Exception e) {
            // 发送消息失败处理，但用户注册核心流程已成功，不应回滚。记录日志并可能通过其他方式补偿。
            log.error("用户注册成功，但发送通知消息失败，用户ID: {}", userId, e);
        }
        // return user;
        return new User(userId, userDTO.getUsername());
    }
}
